import { defineStore } from "pinia"
import request from '@/utils/request'
import { ApiRes, CategoryItem, SubCategory, TopCategory } from '@/types/data'
import {topCategory} from '../constants'

const defaultCategory = topCategory.map(item =>({name:item}))
export default defineStore('category',{
  state:()=>({
        list:defaultCategory as CategoryItem[],
        topCategory: {} as TopCategory,
        subCategory: {} as SubCategory
  }),
  actions:{

   async getAllcategory(){
    const res = await request.get<ApiRes<CategoryItem[]>>('/home/category/head')
     res.data.result.forEach(item => item.open = false)
    // console.log(res);
    this.list = res.data.result
    
   },
   show(id:string){
   const item =   this.list.find(item => item.id === id)
     item && (item.open = true)
   },
   hide(id:string){
    const item =   this.list.find(item => item.id === id)
    item && (item.open = false)
   },
   async getTopCategory(id: string) {
    const res = await request.get<ApiRes<TopCategory>>('/category', {
      params: {
        id,
      },
    })
 
    this.topCategory = res.data.result
  },

  async getSubCategory(id: string) {
    const res = await request.get<ApiRes<SubCategory>>('/category/sub/filter', {
      params: {
        id
      }
    })
    this.subCategory = res.data.result
  },
  resetSubCategory() {
    this.subCategory = {} as SubCategory
  },
  resetTopCategory() {
    this.topCategory = {} as TopCategory
  },

  }

})